<?php

namespace App\Http\Logic\PC;

use App;
use App\Common\Status;
use Illuminate\Support\Facades\DB;
use function PHPUnit\Framework\isEmpty;

class CityLogic
{
    public static function getCity($pid = 0)
    {
        $ct = DB::table("recruit_job as a")
            ->distinct("a.city")
            ->leftJoin("recruit_city as b","a.city", "=", "b.id")
            ->select(["b.id", "b.name", "b.parentid", "b.index", "b.leveltype", "b.pinyin as url"])
            ->where([["a.show","=",Status::PASS],["a.status","=",Status::PASS]])
            ->get();

        $hotctIds = DB::table("recruit_job")
            ->select(["city", DB::raw('count(city) as city_num')])
            ->whereNotNull('city')
            ->where([["show","=",Status::PASS],["status","=",Status::PASS]])
            ->groupBy("city")
            ->orderByDesc("city_num")
            ->limit(10)
            ->get();

        $hotct = DB::table("recruit_city")
            ->select(["id", "name", "parentid", "index", "leveltype", "pinyin as url"])
            ->whereIn("id",$hotctIds->pluck("city"))
            ->get();


        $resCt = $resHotct = $siteList =[];
        if ($ct->isNotEmpty()) {
            $data   = $ct->map(function ($value) {
                return (array)$value;
            })->toArray();
            $resCt = self::buildCityData($data);

            // ----------
            $sdata   = $ct->map(function ($value) {
                return (array)$value;
            })->toArray();

            # 获取所有地级市上级id
            $pids = array_unique(array_column($sdata, "parentid"));

            # 获取省份
            $province = DB::table("recruit_city")->whereIn("id", $pids)
                ->select(["id", "name", "parentid", "index", "leveltype", "pinyin as url"])->get();

            $sdata = array_merge($sdata,$province->map(function ($value) {
                return (array)$value;
            })->toArray());

            $siteList = self::getcityTree($sdata);
        }

        return ["siteList" => $siteList,"siteGroup" => $resCt, "hotCitySites" => $hotct];
    }


    private static function buildCityData($data)
    {
        $result = [];
        foreach ($data as $item) {
            $index = $item['index'];
            unset($item['index']);
            if (!isset($result[$index])) {
                $result[$index] = [
                    "firstChar" => $index,
                    "cityList"  => [],
                ];
            }
            $result[$index]["cityList"][] = $item;
        }

        # 删除索引
        return array_values($result);
    }


    /**
     * @param array $citys
     * @return array
     */
    public static function getcityTree(array $citys)
    {
        $data = [];
        foreach ($citys as $city) {
            if ($city['leveltype'] === 1) {
                $data[] = self::getChildren($citys, $city);
            }
        }
        return $data;
    }


    /**
     * @param $citys
     * @param $city
     * @return mixed
     */
    public static function getChildren($citys, $city)
    {
        $c = [];
        foreach ($citys as $children) {
            if ($city['id'] === $children['parentid']) {
                $c[] = self::getChildren($citys, $children);
            }
        }
        $city['children'] = $c;
        return $city;
    }
}
